package com.ld.oauth2.controller;

import com.ld.common.ResultInfoFactory;
import com.ld.oauth2.model.bo.RegisterUser;
import com.ld.oauth2.model.po.SysUser;
import com.ld.oauth2.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 校验名称是否已经存在 false不存在 true已经存在
     * @param username
     * @return
     */
    @RequestMapping("/api/user/username")
    public ResultInfoFactory<Boolean> checkUserName(String username) {
        SysUser sysUser = sysUserService.findByUsername(username);
        Boolean flag = Objects.isNull(sysUser) ? false : true;
        return ResultInfoFactory.success(flag);
    }

    @RequestMapping("/api/register")
    @PostMapping
    public ResultInfoFactory<Boolean> register(@RequestBody RegisterUser registerUser) {
        sysUserService.register(registerUser);
        return ResultInfoFactory.success();
    }
    @RequestMapping("/refresh")
    public ResultInfoFactory<Map<String,Object>> refresh(String refreshToken){
        return ResultInfoFactory.success(sysUserService.refresh(refreshToken));
    }


}
